<?php
namespace app\admin\service\agent;

use app\admin\model\agent\MoneyLog as Model;

class AgentMoneyLog 
{
    private Model $model;

    public function __construct(){
        $this->model = new Model;
    }

    /**
     * 创建变更记录
     * 
     * @param int $agent_id // 代理商ID
     * @param string $symbol 符号 + 或者-
     * @param float $money 金额
     * @param string $remake 备注
     * @param array $option 其他可添加的数据
     *      [
     *          'type'  =>  '1 订单收入 2提现 3 增加余额 4 减少余额'
     *          'order_id'  =>  订单id
     *          'withdrawal_id' =>  提现表ID
     *      ] 
     * 
     */
    public function create($agent_id, $symbol, $money, $remake, $option =[]){

        $agent = Db::table("ba_admin")
        ->where([
            "id"    =>  $agent_id,
        ])
        ->find();
        if(!$agent){
            throw new \Exception("代理商数据获取失败");
        }
        switch($symbol){
            case "+":
                $after = floatval($money) + floatval($agent['money']);
                $type = 3;
                break;
            case "-":
                $after = floatval($money) - floatval($agent['money']);
                $type = 4;
                break;
            default:
                throw new \Exception("符号匹配错误");
        }

        $this->model
        ->create([
            'agent_id'  =>  $agent_id, //代理商ID
            'money' =>  $money,
            'before'    =>  $agent['money'],
            'after'     =>  $after,
            'memo'      =>  $option['memo']??'',
            'order_id'      =>  $option['order_id']??'',
            'withdrawal_id'      =>  $option['withdrawal_id']??'',
            'type'      =>  $option['type']??$type,
        ]);
        
    }

    // `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',
    // `agent_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '代理商ID',
    // `money` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '变更余额',
    // `before` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '变更前余额',
    // `after` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '变更后余额',
    // `memo` varchar(1000) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '备注',
    // `create_time` datetime DEFAULT NULL COMMENT '创建时间',
    // `type` tinyint(1) NOT NULL COMMENT '1 订单收入 2提现 3 增加余额 4 减少余额',
    // `order_id` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '订单id',
    // `withdrawal_id` int(11) NOT NULL DEFAULT '0' COMMENT '关联提现',

}